﻿using System;
using System.Linq;
using System.Security.Principal;
using Equation.Enterprise.Framework.Domain.Data;
using Equation.Enterprise.Framework.Domain.Model;

namespace ENail.Domain.Core
{
    [Serializable]
    public class User : EntityWithTypedId<Int16>, IPrincipal
    {
        private readonly IRepository<NailSalon> _nailSalonRepository;

        public string Username { get; set; }
        public string Password { get; set; }
        public virtual NailSalon NailSalonContext { get; set; }
        public NailSalon NailSalon
        {
            get
            {
                var query = _nailSalonRepository.AsNoTracking<NailSalon>();
                var nailSalon = query.FirstOrDefault(ns => 
                        (ns.Users.FirstOrDefault(u=>u.Username==Username) != null));
                return nailSalon;
            }
            set { NailSalonContext = value; }
        }

        public User(string username, string password)
            : this(username, password, null)
        {
        }
        
        public User(string username, string password, NailSalon nailSalon) : this()
        {
            Username = username;
            Password = password;
            NailSalon = nailSalon;
        }

        public User()
        {
            _nailSalonRepository = new DbRepository<NailSalon>();
        }

        #region IPrincipal Implementation

        //There is no role for now so always returns true
        public virtual bool IsInRole(string role)
        {
            return true;
        }

        public virtual IIdentity Identity { get; set; }

        #endregion
    }
}
